<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<div th:replace="admin/module/_header"></div>
<div class="wrapper">
    <!-- 菜单栏模块 -->
    <div th:replace="admin/module/_sidebar"></div>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">

        <section class="content-header">
            <h1 style="display: inline-block;">店铺发布</h1>
            <ol class="breadcrumb">
                <li>
                    <a data-pjax="true" href="#"><i
                            class="fa fa-dashboard"></i> 首页</a>
                </li>
                <li>
                    <a data-pjax="true" href="#">店铺管理</a>
                </li>
                <li class="active">修改</li>
            </ol>
        </section>
        <section class="content">
            <div class="row">
                <div class="col-md-12">
                    <!-- general form elements -->
                    <div class="box box-primary">
                        <!-- form start -->

                        <div class="box-body">
                            <div class="row">
                                <form id="form">
                                    <div class="col-md-6">

                                        <input type="hidden" name="id" th:value="${item.id}">

                                        <div class="form-group">
                                            <div class="col-md-4">
                                                <label>店铺照片</label>
                                                <div class="input-group">
                                                    <input type="hidden" class="form-control"
                                                           id="photo"
                                                           name="photo" th:value="${item.photo}">
                                                    <span class="input-group-btn">
                                                    <input type="file" id="file" accept=".png, .jpg, .jpeg"
                                                           style="display: none">
                                                        <button onclick="$('#file').click();" class="btn btn-default "
                                                                type="button">选择图片</button>
                                                    </span>
                                                </div>
                                            </div>
                                            <div class="col-md-8">
                                                <img th:src="${item.photo}" id="photoImg" style="width: 200px;" alt="">
                                            </div>
                                        </div>

                                        <div class="form-group">
                                            <label>店铺名称</label>
                                            <input type="text" class="form-control" id="name" name="name"
                                                   th:value="${item.name}">
                                        </div>

                                        <div class="form-group">
                                            <label>店铺责任人姓名</label>
                                            <input type="text" class="form-control" id="businessName"
                                                   name="businessName"
                                                   th:value="${item.businessName}">
                                        </div>

                                        <div class="form-group">
                                            <label>店铺责任人手机号</label>
                                            <input type="text" class="form-control" id="businessPhone"
                                                   name="businessPhone"
                                                   th:value="${item.businessPhone}">
                                        </div>
                                        <div class="form-group">
                                            <label>店铺责地址</label>
                                            <input type="text" class="form-control" id="address"
                                                   name="address"
                                                   th:value="${item.address}">
                                        </div>

                                        <div class="form-group">
                                            <label>人均消费金额</label>
                                            <input type="number" class="form-control" id="avgPrice"
                                                   name="avgPrice"
                                                   th:value="${item.avgPrice}">
                                        </div>

                                        <div class="form-group" th:style="${!isAdmin ? 'display: none;' : ''}">
                                            <label>店铺授权管理账号</label>
                                            <input type="text" class="form-control" id="businessAccount"
                                                   name="businessAccount"
                                                   th:value="${item.user == null ? '' : item.user.userName}">
                                        </div>




                                    </div>
                                </form>


                            </div>

                            <!-- /.box-body -->
                            <div class="box-footer">
                                <button type="button" class="btn btn-primary btn-sm" onclick="save()">发布</button>
                                <a href="/admin/store" type="button" class="btn btn-info btn-sm">返回</a>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </section>
        <script>


            /**
             * 提交
             * @param status 状态
             */
            function save() {
                let businessPhone = $('#businessPhone').val();
                let businessName = $('#businessName').val();
                let businessAccount = $('#businessAccount').val();
                let name = $('#name').val();
                let phone = $('#phone').val();
                let photo = $('#photo').val();
                let address = $('#address').val();
                if (businessPhone == '' || businessName == '' || businessAccount == '' ||
                    name == '' || phone == '' || photo == '' || address == ''
                ) {
                    showErrorMsg('请输入完整信息');
                    return;
                }
                $.ajax({
                    type: 'POST',
                    url: '/admin/store/save',
                    async: false,
                    data: $('#form').serialize(),
                    success: function (data) {
                        if (data.code == 1) {
                            showMsgAndRedirect("保存成功", "success", 1000, "/admin/store/list");
                        } else {
                            showMsg(data.msg, "error", 1000);
                        }
                    }
                });
            }


            $('body').on('change', '#file', function () {
                var formData = new FormData();
                var files = $($(this))[0].files[0];
                formData.append("file", files);
                $.ajax({
                    url: '/admin/file/upload',
                    type: 'POST',
                    data: formData,
                    processData: false,
                    contentType: false,
                    dataType: 'json',
                    success: function (res) {
                        console.log(res);
                        $('#photo').val(res.link);
                        $('#photoImg').attr('src', res.link);
                    }
                    , error: function (res) {
                    }
                });
            })


        </script>
    </div>

</div>
<div th:replace="admin/module/_footer"></div>